home *** CD-ROM | disk | FTP | other *** search
/ Best of Shareware / Best of PC Windows Shareware 1.0 - Wayzata Technology (7111) (1993).iso / mac / DOS / CAD_CAM / A7221V1B / INPUT.C < prev    next >
C/C++ Source or Header  |  1992-03-12  |  4KB  |  161 lines

  1. /*
  2.    Module:  input.c
  3.    Date:    3/9/92
  4.    Version: 1.0b
  5.    Author:  Dave Lutz
  6.    Email:   lutz@psych.rochester.edu
  7.    Copyright: 1992 University of Rochester, Psychology Dept.
  8.  
  9.    Disclaimer:  This software is distributed free of charge.  As such, it
  10.                 comes with ABSOLUTELY NO WARRANTY.  The user of the software
  11.                 assumes ALL RISKS associated with its use.
  12.  
  13.                 Your rights to modify and/or distribute this software are
  14.                 outlined in the file SEND7221.DOC.
  15.  
  16.    Purpose: This module provides the input routines for the PC -> HP7221
  17.             controller.  It provides the routines needed by the controller
  18.             to open the input file and read data.
  19.  
  20.    Functions Provided:
  21.  
  22.             openin
  23.             closein
  24.             fillbuff
  25.  
  26.    Functions Required:
  27.  
  28.             none
  29.  
  30. */
  31.  
  32.  
  33.  
  34.  
  35. #include <fcntl.h>
  36. #include <sys\types.h>
  37. #include <sys\stat.h>
  38. #include <stdlib.h>
  39. #include <malloc.h>
  40. #include <io.h>
  41. #include "retcodes.h"
  42. #include "input.h"
  43.  
  44.  
  45. /*
  46.    Function: openin
  47.    Purpose:  Allocate the input buffer and open the input file for reading.
  48.  
  49.    Pre: name is a pointer to an ascii string containing the name of the 
  50.         input file.
  51.         pltfpp is a pointer to storage for a pointer to a PLTFILE.
  52.  
  53.    Post:  The input buffer is allocated.
  54.           The input file is opened for reading.
  55.           If the buffer can't be allocated, *pltfpp is set to NULL and
  56.           NOBUFF is returned.
  57.           If the input file can't be opened, *pltfpp is set to NULL and
  58.           BADOPEN is returned.
  59.           Otherwise, TRUE is returned.
  60. */
  61.  
  62. int openin (name,pltfpp)
  63.    char *name;
  64.    PLTFILE **pltfpp;
  65. {
  66.    /* first try to allocate a PLTFILE */
  67.    *pltfpp = (PLTFILE *) malloc (sizeof (PLTFILE));
  68.    if (*pltfpp == (PLTFILE *)NULL)
  69.       return (NOBUFF);
  70.  
  71.    /* now try to allocate the buffer */
  72.    (*pltfpp)->buf = (char *) malloc ((size_t)(BUFFSIZE*sizeof(char)));  
  73.    if ((*pltfpp)->buf == (char *)NULL) {
  74.       free ((void *)*pltfpp);
  75.       *pltfpp = (PLTFILE *)NULL;
  76.       return (NOBUFF);
  77.    }
  78.  
  79.    /* now attempt to open the input file */
  80.    (*pltfpp)->fd = open (name,O_RDONLY|O_BINARY);
  81.    if ((*pltfpp)->fd < 0) {
  82.       free ((void *)(*pltfpp)->buf);
  83.       free ((void *)*pltfpp);
  84.       *pltfpp = (PLTFILE *)NULL;
  85.       return (BADOPEN);
  86.    }
  87.  
  88.    /* now set buf_ctr to zero so the first read will cause the buffer to be 
  89.       filled from the input file
  90.    */
  91.    (*pltfpp)->buf_ctr = 0;
  92.    (*pltfpp)->eof=FALSE;
  93.    return (TRUE);
  94. }
  95.  
  96.  
  97.  
  98. /*
  99.    Function: closein
  100.    Purpose:  Discard the input buffer, close the input file, and deallocate 
  101.              the input buffer.
  102.  
  103.    Pre:  pltfpp is a pointer to a pointer to a PLTFILE.
  104.          If *pltfpp does not point to an open PLTFILE, it is NULL.
  105.  
  106.    Post:  If *pltfpp = NULL, TRUE is returned.
  107.           Otherwise, the contents of the input buffer are discarded, the 
  108.           input file is closed, and the input buffer is deallocated.
  109.           If an error occurs during the above process, FALSE is returned.
  110. */
  111.  
  112. int closein(pltfpp)
  113.    PLTFILE **pltfpp;
  114. {
  115.    if (*pltfpp == (PLTFILE *)NULL)
  116.       return (TRUE);
  117.    free ((void *)(*pltfpp)->buf);
  118.    if (close((*pltfpp)->fd) < 0) {
  119.        free ((void *)*pltfpp);
  120.        return (FALSE);
  121.    }
  122.    free ((void *)*pltfpp);
  123.    return (TRUE);
  124. }
  125.  
  126.  
  127.  
  128. /*
  129.    Function: fillbuff
  130.    Purpose:  Read a block of data from the input file, filling the buffer
  131.              in the PLTFILE.
  132.  
  133.    Pre:  pltfp is a pointer to a PLTFILE that has been opened for reading.
  134.  
  135.    Post: The input file is read to refill the buffer in PLTFILE.
  136.          If end_of_file has already been reached, 0 is returned.
  137.          If an error occures, -1 is returned.
  138.          Otherwise, the number of bytes read from the file is returned.
  139. */
  140.  
  141. int fillbuff (pltfp)
  142.    PLTFILE *pltfp;
  143. {
  144.    if (pltfp->eof == TRUE)
  145.       return (0);
  146.  
  147.    switch (pltfp->buf_ctr = read(pltfp->fd,pltfp->buf, (unsigned)BUFFSIZE)) {
  148.       case 0:
  149.          pltfp->eof = TRUE;
  150.          return (0);
  151.          break;
  152.       case -1:
  153.          return (-1);
  154.          break;
  155.       default:
  156.          /* pltfp->buf_ctr was set by the read funtion */
  157.          break;
  158.    }
  159.    return (pltfp->buf_ctr);
  160. }
  161.